package com.coffee.core.util;

import org.apache.commons.lang3.StringUtils;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 身份证号码校验工具
 *
 * @author Xulg
 * Created in 2018-08-27 9:44
 */
public class IDCardUtils {

    /**
     * 15位身份证号
     */
    private static final Integer FIFTEEN_ID_CARD=15;
    /**
     * 18位身份证号
     */
    private static final Integer EIGHTEEN_ID_CARD=18;

    @SuppressWarnings("all")
    private final static Map<Integer, String> zoneNum = new HashMap<Integer, String>() {
        {
            put(11, "北京");
            put(12, "天津");
            put(13, "河北");
            put(14, "山西");
            put(15, "内蒙古");
            put(21, "辽宁");
            put(22, "吉林");
            put(23, "黑龙江");
            put(31, "上海");
            put(32, "江苏");
            put(33, "浙江");
            put(34, "安徽");
            put(35, "福建");
            put(36, "江西");
            put(37, "山东");
            put(41, "河南");
            put(42, "湖北");
            put(43, "湖南");
            put(44, "广东");
            put(45, "广西");
            put(46, "海南");
            put(50, "重庆");
            put(51, "四川");
            put(52, "贵州");
            put(53, "云南");
            put(54, "西藏");
            put(61, "陕西");
            put(62, "甘肃");
            put(63, "青海");
            put(64, "新疆");
            put(71, "台湾");
            put(81, "香港");
            put(82, "澳门");
            put(91, "外国");
        }
    };

    private final static int[] PARITYBIT = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};

    private final static int[] POWER_LIST = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};

    /**
     * 校验身份证号码格式
     *
     * @param certNo 身份证号码
     * @return 身份证号格式是否正确
     */
    public static boolean isIDCard(String certNo) {
        if (certNo == null || (certNo.length() != 15 && certNo.length() != 18)) {
            return false;
        }
        final char[] cs = certNo.toUpperCase().toCharArray();
        //校验位数
        int power = 0;
        for (int i = 0; i < cs.length; i++) {
            if (i == cs.length - 1 && cs[i] == 'X') {
                //最后一位可以是X或x
                break;
            }
            if (cs[i] < '0' || cs[i] > '9') {
                return false;
            }
            if (i < cs.length - 1) {
                power += (cs[i] - '0') * POWER_LIST[i];
            }
        }
        //校验区位码
        if (!zoneNum.containsKey(Integer.valueOf(certNo.substring(0, 2)))) {
            return false;
        }
        //校验年份
        String year = certNo.length() == 15
                ? getIdcardCalendar() + certNo.substring(6, 8)
                : certNo.substring(6, 10);
        final int iyear = Integer.parseInt(year);
        if (iyear < 1900 || iyear > Calendar.getInstance().get(Calendar.YEAR)) {
            //1900年的PASS，超过今年的PASS
            return false;
        }
        //校验月份
        String month = certNo.length() == 15
                ? certNo.substring(8, 10)
                : certNo.substring(10, 12);
        final int imonth = Integer.parseInt(month);
        if (imonth < 1 || imonth > 12) {
            return false;
        }
        //校验天数     
        String day = certNo.length() == 15
                ? certNo.substring(10, 12)
                : certNo.substring(12, 14);
        final int iday = Integer.parseInt(day);
        if (iday < 1 || iday > 31) {
            return false;
        }
        //校验"校验码"
        return certNo.length() == 15 || cs[cs.length - 1] == PARITYBIT[power % 11];
    }

    private static int getIdcardCalendar() {
        GregorianCalendar curDay = new GregorianCalendar();
        int curYear = curDay.get(Calendar.YEAR);
        return Integer.parseInt(String.valueOf(curYear).substring(2));
    }

    public static String getBirthday(String IDCard){
        String birthday="";
        String year="";
        String month="";
        String day="";
        if (StringUtils.isNotBlank(IDCard)){
            //15位身份证号
            if (IDCard.length() == FIFTEEN_ID_CARD){
                // 身份证上的年份(15位身份证为1980年前的)
                year = "19" + IDCard.substring(6, 8);
                //身份证上的月份
                month = IDCard.substring(8, 10);
                //身份证上的日期
                day= IDCard.substring(10, 12);
                //18位身份证号
            }else if(IDCard.length() == EIGHTEEN_ID_CARD){
                // 身份证上的年份
                year = IDCard.substring(6).substring(0, 4);
                // 身份证上的月份
                month = IDCard.substring(10).substring(0, 2);
                //身份证上的日期
                day=IDCard.substring(12).substring(0,2);
            }
            birthday=year+"年"+month+"月"+day+"日";
        }
        return birthday;
    }

}